home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 14 The Tessellation Stages / BasicTessellation / Shaders / Tessellation.hlsl < prev   
Encoding:
Text File  |  2016-03-02  |  4.0 KB  |  174 lines

  1. //***************************************************************************************
  2. // Tessellation.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5.  
  6. // Include structures and functions for lighting.
  7. #include "LightingUtil.hlsl"
  8.  
  9. Texture2D    gDiffuseMap : register(t0);
  10.  
  11.  
  12. SamplerState gsamPointWrap        : register(s0);
  13. SamplerState gsamPointClamp       : register(s1);
  14. SamplerState gsamLinearWrap       : register(s2);
  15. SamplerState gsamLinearClamp      : register(s3);
  16. SamplerState gsamAnisotropicWrap  : register(s4);
  17. SamplerState gsamAnisotropicClamp : register(s5);
  18.  
  19. // Constant data that varies per frame.
  20. cbuffer cbPerObject : register(b0)
  21. {
  22.     float4x4 gWorld;
  23.     float4x4 gTexTransform;
  24. };
  25.  
  26. // Constant data that varies per material.
  27. cbuffer cbPass : register(b1)
  28. {
  29.     float4x4 gView;
  30.     float4x4 gInvView;
  31.     float4x4 gProj;
  32.     float4x4 gInvProj;
  33.     float4x4 gViewProj;
  34.     float4x4 gInvViewProj;
  35.     float3 gEyePosW;
  36.     float cbPerObjectPad1;
  37.     float2 gRenderTargetSize;
  38.     float2 gInvRenderTargetSize;
  39.     float gNearZ;
  40.     float gFarZ;
  41.     float gTotalTime;
  42.     float gDeltaTime;
  43.     float4 gAmbientLight;
  44.  
  45.     float4 gFogColor;
  46.     float gFogStart;
  47.     float gFogRange;
  48.     float2 cbPerObjectPad2;
  49.  
  50.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  51.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  52.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  53.     // are spot lights for a maximum of MaxLights per object.
  54.     Light gLights[MaxLights];
  55. };
  56.  
  57. cbuffer cbMaterial : register(b2)
  58. {
  59.     float4   gDiffuseAlbedo;
  60.     float3   gFresnelR0;
  61.     float    gRoughness;
  62.     float4x4 gMatTransform;
  63. };
  64.  
  65. struct VertexIn
  66. {
  67.     float3 PosL    : POSITION;
  68. };
  69.  
  70. struct VertexOut
  71. {
  72.     float3 PosL    : POSITION;
  73. };
  74.  
  75. VertexOut VS(VertexIn vin)
  76. {
  77.     VertexOut vout;
  78.     
  79.     vout.PosL = vin.PosL;
  80.  
  81.     return vout;
  82. }
  83.  
  84. struct PatchTess
  85. {
  86.     float EdgeTess[4]   : SV_TessFactor;
  87.     float InsideTess[2] : SV_InsideTessFactor;
  88. };
  89.  
  90. PatchTess ConstantHS(InputPatch<VertexOut, 4> patch, uint patchID : SV_PrimitiveID)
  91. {
  92.     PatchTess pt;
  93.     
  94.     float3 centerL = 0.25f*(patch[0].PosL + patch[1].PosL + patch[2].PosL + patch[3].PosL);
  95.     float3 centerW = mul(float4(centerL, 1.0f), gWorld).xyz;
  96.     
  97.     float d = distance(centerW, gEyePosW);
  98.  
  99.     // Tessellate the patch based on distance from the eye such that
  100.     // the tessellation is 0 if d >= d1 and 64 if d <= d0.  The interval
  101.     // [d0, d1] defines the range we tessellate in.
  102.     
  103.     const float d0 = 20.0f;
  104.     const float d1 = 100.0f;
  105.     float tess = 64.0f*saturate( (d1-d)/(d1-d0) );
  106.  
  107.     // Uniformly tessellate the patch.
  108.  
  109.     pt.EdgeTess[0] = tess;
  110.     pt.EdgeTess[1] = tess;
  111.     pt.EdgeTess[2] = tess;
  112.     pt.EdgeTess[3] = tess;
  113.     
  114.     pt.InsideTess[0] = tess;
  115.     pt.InsideTess[1] = tess;
  116.     
  117.     return pt;
  118. }
  119.  
  120. struct HullOut
  121. {
  122.     float3 PosL : POSITION;
  123. };
  124.  
  125. [domain("quad")]
  126. [partitioning("integer")]
  127. [outputtopology("triangle_cw")]
  128. [outputcontrolpoints(4)]
  129. [patchconstantfunc("ConstantHS")]
  130. [maxtessfactor(64.0f)]
  131. HullOut HS(InputPatch<VertexOut, 4> p, 
  132.            uint i : SV_OutputControlPointID,
  133.            uint patchId : SV_PrimitiveID)
  134. {
  135.     HullOut hout;
  136.     
  137.     hout.PosL = p[i].PosL;
  138.     
  139.     return hout;
  140. }
  141.  
  142. struct DomainOut
  143. {
  144.     float4 PosH : SV_POSITION;
  145. };
  146.  
  147. // The domain shader is called for every vertex created by the tessellator.  
  148. // It is like the vertex shader after tessellation.
  149. [domain("quad")]
  150. DomainOut DS(PatchTess patchTess, 
  151.              float2 uv : SV_DomainLocation, 
  152.              const OutputPatch<HullOut, 4> quad)
  153. {
  154.     DomainOut dout;
  155.     
  156.     // Bilinear interpolation.
  157.     float3 v1 = lerp(quad[0].PosL, quad[1].PosL, uv.x); 
  158.     float3 v2 = lerp(quad[2].PosL, quad[3].PosL, uv.x); 
  159.     float3 p  = lerp(v1, v2, uv.y); 
  160.     
  161.     // Displacement mapping
  162.     p.y = 0.3f*( p.z*sin(p.x) + p.x*cos(p.z) );
  163.     
  164.     float4 posW = mul(float4(p, 1.0f), gWorld);
  165.     dout.PosH = mul(posW, gViewProj);
  166.     
  167.     return dout;
  168. }
  169.  
  170. float4 PS(DomainOut pin) : SV_Target
  171. {
  172.     return float4(1.0f, 1.0f, 1.0f, 1.0f);
  173. }
  174.